package com.dense.kuiniu.shelfcard.business;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.shelfcard.R;
import com.dense.kuiniu.shelfcard.base.BaseFragment;
import com.dense.kuiniu.shelfcard.card.iccard.CardOutPut;
import com.dense.kuiniu.shelfcard.card.iccard.HFCardReader;
import com.dense.kuiniu.shelfcard.card.iccard.KNCardBinder;
import com.dense.kuiniu.shelfcard.card.iccard.ResultListener;
import com.dense.kuiniu.shelfcard.card.iccard.TTCardOutPut;
import com.dense.kuiniu.shelfcard.card.idcard.IDCardInfo;
import com.dense.kuiniu.shelfcard.entity.BindInfo;
import com.dense.kuiniu.shelfcard.utils.timer.CountDownView;
import com.dense.kuiniu.shelfcard.utils.timer.CounterController;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/9/23
 */
public class OutputCardDelegate extends BaseFragment implements KNCardBinder.BindObserver {
    private static final String KEY = "idCard";

    public static OutputCardDelegate getInstance(IDCardInfo idCard) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY, idCard);
        OutputCardDelegate delegate = new OutputCardDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.iv_card)
    ImageView mIvCard;
    @BindView(R.id.tv_msg)
    TextView mTvMsg;
    @BindView(R.id.view_count_down)
    CountDownView mCountDownView;
    @BindView(R.id.et_card_no)
    EditText mEtCardNo;
    private IDCardInfo mIDCard;
    private CardOutPut mCardOutPut;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mIDCard = bundle.getParcelable(KEY);
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_output_card;
    }

    @OnClick({R.id.tv_confirm})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_confirm:
                pop();
                break;

        }
    }

    @Override
    protected void init() {
        mCountDownView.start(new CounterController.OnCompleteListener() {
            @Override
            public void onComplete() {
                pop();
            }
        });
        mCardOutPut = new TTCardOutPut("/dev/ttyS0", new HFCardReader(), new KNCardBinder(mIDCard, this));
        mCardOutPut.getCard(new ResultListener() {
            @Override
            public void result(String cardNo) {
                ToastUtils.showShort(cardNo);
            }

            @Override
            public void error(String msg) {
                ToastUtils.showShort(msg);
            }
        });
    }

    @Override
    public void succeed(BindInfo info) {
        CardOutputInfoDelegate delegate = findFragment(CardOutputInfoDelegate.class);
        if (delegate == null) {
            delegate = CardOutputInfoDelegate.getInstance(info);
        }
        startWithPop(delegate);
    }

    @Override
    public void failed(String msg) {
        mTvMsg.setText(msg);
        mTvMsg.setTextColor(getResources().getColor(R.color.colorFailed));
        mIvCard.setBackgroundResource(R.mipmap.ic_error);
        mTvConfirm.setVisibility(View.VISIBLE);
        mTvConfirm.setBackgroundResource(R.drawable.shape_bg_exit);
    }
}